//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#pragma package(smart_init)
#pragma link "CComboBx"
#pragma link "CEdit"
#pragma resource "*.dfm"

//---------------------------------------------------------------------------
#include "vSAjoutVersion.h"
//---------------------------------------------------------------------------
//          CONSTRUCTEUR
//---------------------------------------------------------------------------
__fastcall TAjoutVersion::TAjoutVersion(TComponent* Owner)
    : TForm(Owner)
{
    TZZMySqlQuery * MaRequete = NULL;
    try {
        dmWebHelp = NULL;
        dmWebHelp = new TdmWebHelp((TComponent*)0);


        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text = "select log_nom, log_code from logiciels";
        MaRequete->Open();
        MaRequete->First();

        while(!MaRequete->Eof) {
            cbLogiciel->Items->AddObject( asRequete(0), (TObject*)nRequete(1) );
            MaRequete->Next();
        }
        MaRequete->Sql->Clear();
        MaRequete->Close();

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
}
//---------------------------------------------------------------------------
__fastcall TAjoutVersion::~TAjoutVersion()
{
    try {
        if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//          BOUTON OK = INSERTION D UNE NOUVELLE VERSION
//---------------------------------------------------------------------------
void __fastcall TAjoutVersion::bbOkClick(TObject *Sender)
{
    CHistorique * Hist = NULL;
    try {
        int idx = cbLogiciel->ItemIndex;
        int Log_Code = 0;

        if( idx != -1 && !Trim(eVersion->Text).IsEmpty()) {
            Log_Code = (int)cbLogiciel->Items->Objects[idx];
            if(!VerifierExistance(Log_Code,eVersion->Text)) {
                dmWebHelp->tVersions->Active = true;
                dmWebHelp->tVersions->Insert();
                ntVersions(LOG_CODE)    =   Log_Code;
                astVersions(VER_NOM)    =   eVersion->Text;
                dmWebHelp->tVersions->Post();
                dmWebHelp->trWebHelp->Commit();
                ModalResult = mrOk;

                Hist = new CHistorique();
                Hist->AjouterOperation(Histo_AJOUT_VER, cbLogiciel->Items->Strings[cbLogiciel->ItemIndex],eVersion->Text );

            }else {
                MessageDlg("Logiciel et version aide déjà existante", mtWarning, TMsgDlgButtons() << mbOK, 0);
                ModalResult = mrNone;
            }
        }else {
            MessageDlg("Veuillez sélectionner un logiciel et lui affilier une version aide", mtWarning, TMsgDlgButtons() << mbOK, 0);
            ModalResult = mrNone;
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(Hist) {delete Hist; Hist = NULL;}
}
//---------------------------------------------------------------------------
//      VERIFIER SI LOGICIEL ET VERSION AIDE DEJA INSERER
//---------------------------------------------------------------------------
bool __fastcall TAjoutVersion::VerifierExistance(   const int &nLogiciel,
                                                    const AnsiString &asVersion)
{
    bool bRetour = false;
    TZZMySqlQuery * MaRequete = NULL;
    try {
        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text = "select log_code, ver_nom from versions";
        MaRequete->Open();
        MaRequete->First();

        if( (nLogiciel == nRequete(0)) && (asVersion == asRequete(1)) )
            bRetour = true;

        MaRequete->Sql->Clear();
        MaRequete->Close();

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        bRetour = false;}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    return bRetour;
}
//---------------------------------------------------------------------------
//                      Evenement combobox
//---------------------------------------------------------------------------
void __fastcall TAjoutVersion::cbLogicielChange(TObject *Sender)
{
    try {
        AnsiString asNomVersionDest(EmptyStr);
        int i = GetMaxVersion((int)cbLogiciel->Items->Objects[cbLogiciel->ItemIndex]);
        eVersion->Text = asNomVersionDest.sprintf( "%03d", ++i );
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
    }
}
//---------------------------------------------------------------------------
//       Vérifier l'existance d'une version par rapport à un logiciel
//---------------------------------------------------------------------------
bool __fastcall TAjoutVersion::VerifVerNom(const AnsiString &asVerNom)
{
    bool bRetour = false;
    TZZMySqlQuery * MaRequete = NULL;
    try {
        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        //Insertion dans la table référence
        //---------------------------------
        MaRequete->Sql->Text = (AnsiString)"select ver_nom from versions where ver_nom = "+QuotedStr(asVerNom)+" and log_code = "+(int)cbLogiciel->Items->Objects[cbLogiciel->ItemIndex];
        MaRequete->Open();
        MaRequete->First();

        if(!asRequete(0).IsEmpty())
            bRetour = true;

        MaRequete->Sql->Clear();
        MaRequete->Close();

    }catch ( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        bRetour = false;}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    return bRetour;
}
//---------------------------------------------------------------------------
//       Obtenir la derniere version par rapport à un logiciel
//---------------------------------------------------------------------------
int  __fastcall TAjoutVersion::GetMaxVersion( const int &nLogCode )
{
    int nMaxVersion = 0;
    TZZMySqlQuery * MaRequete = NULL;
    try {
        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        //Insertion dans la table référence
        //---------------------------------
        MaRequete->Sql->Text = (AnsiString)"select max(ver_nom) from versions where log_code = "+nLogCode;
        MaRequete->Open();
        MaRequete->First();

        if( !asRequete(0).IsEmpty() )
            nMaxVersion = asRequete(0).ToInt();

        MaRequete->Sql->Clear();
        MaRequete->Close();

    }catch ( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        nMaxVersion = 0;}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    return nMaxVersion;
}
//---------------------------------------------------------------------------

